{
capture drop  sbjt_in_sample
capture drop sample
gen sample=(S_pds==1 & n_ud==1) 	
bys gr_sbjt: egen sbjt_in_sample=max(sample)

sort $isid_vars  
capture drop TEMP_SAMPLE
gen TEMP_SAMPLE=(n_sbjt==1 & sbjt_in_sample==1)   
cap drop gr_sd
egen gr_sd=group(s_id top_dest_b)
  
capture drop xx_bjt
capture drop xx_sbjt
bys gr_bjt: egen xx_bjt=total(q_exp)  
bys gr_sbjt: egen xx_sbjt=total(q_exp)   
 
capture drop xx_sbjt_bjt
capture drop max_sh_sbjt_bjt
 
gen xx_sbjt_bjt=xx_sbjt/xx_bjt

bys gr_bjt: egen max_sh_sbjt_bjt=max(xx_sbjt_bjt)

capture drop flag1
gen flag1=(max_sh_sbjt_bjt==xx_sbjt_bjt)  
* This flags the seller with the largest share in the buyer-product-year  

cap drop  xx_sh_sbt_bt
bys gr_bt: egen xx_sh_sbt_bt=max(sh_sbt_bt)
capture drop flag2 
gen flag2=(xx_sh_sbt_bt==sh_sbt_bt) 

label var D_sourcing "\$Relational^D_b\$" 
label var S_sourcing "\$Relational_b\$" 


capture drop sample
gen sample=(n_ud==1 & S_pds==1)

eststo clear
eststo: reghdfe ln_p_sbjo   S_sourcing  ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month     ln_q_sbjo ln_p_inpf_sbjo  ///
if sample==1, ///
abs(gr_sjt top_dest_b cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
     quietly estadd local CTRL2 =".", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjy,d", replace  
  quietly estadd scalar  U =  . , replace    
  
eststo: reghdfe ln_mc_o_B   S_sourcing  ln_q_sb_all sh_s_b sh_b_s age_buysel_month  signatory ///
 ln_q_b_all age_buy_month    ln_q_sbjo ln_p_inpf_sbjo  ///
if sample==1, ///
abs(gr_sjt top_dest_b cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
     quietly estadd local CTRL2 =".", replace   
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjy,d", replace  
  quietly estadd scalar  U =  . , replace    
  
eststo: reghdfe ln_mu_o_B   S_sourcing  ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month    ln_q_sbjo ln_p_inpf_sbjo  ///
if sample==1, ///
abs(gr_sjt top_dest_b cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
    quietly estadd local CTRL2 =".", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjy,d", replace  
  quietly estadd scalar  U =  . , replace      
  
eststo: reghdfe ln_mu_o_B  S_sourcing  ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month ln_p_inpf_sbjo ln_q_sbjo hhi_seas_sb_norm sh_max_seas_sbt seas_largest_sbt  ///
if sample==1, ///
abs(gr_sjt top_dest_b cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
  quietly estadd local CTRL2 ="\scriptsize{Season}", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjy,d", replace  
  quietly estadd scalar  U =  . , replace 

eststo: reghdfe ln_mu_o_B  S_sourcing  ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month ln_p_inpf_sbjo ln_q_sbjo hhi_j_sb_norm sh_max_j_sbt core_product_sb ///
if sample==1, ///
abs(gr_sjt top_dest_b cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
  quietly estadd local CTRL2 ="\scriptsize{Product}", replace  
    quietly estadd local FE ="sjy,d", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd scalar  U =  . , replace 
 
eststo: reghdfe ln_mu_o_B   S_sourcing  ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month   ln_q_sbjo ln_p_inpf_sbjo ln_numf_HS6_UD_imp  ///
if sample==1, ///
abs(gr_sjfot  gr_dq cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
  quietly estadd local CTRL2 ="\scriptsize{Quality}", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjfoy,dq", replace  
  quietly estadd scalar  U =  . , replace        
  
  
eststo: reghdfe ln_mu_o_B   S_sourcing  ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month    ln_q_sbjo ln_p_inpf_sbjo  ///
if sample==1 & flag2==0, ///
abs(gr_sjt top_dest_b cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
  quietly estadd local CTRL2 ="\scriptsize{Small b}", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjy,d", replace  
  quietly estadd scalar  U =  . , replace    
 
 eststo: reghdfe ln_mu_o_B   S_sourcing  ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month    ln_q_sbjo ln_p_inpf_sbjo  ///
if sample==1 & flag1==0, ///
abs(gr_sjt top_dest_b cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
  quietly estadd local CTRL2 ="\scriptsize{Small s}", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjy,d", replace  
  quietly estadd scalar  U =  . , replace     

 
esttab using  "${tables}\Table6.tex", ///
  label replace booktabs b(3) se(3) noconstant obslast substitute(\_ _) ///
      title("Buyers' Sourcing, Markups and Costs" \label{SourcingMuMc}) ///
  drop(ln_q_sb_all sh_s_b sh_b_s age_buysel_months ln_q_b_all age_buy_months  signatory ln_q_sbjo ln_p_inpf_sbjo  ///
  hhi_j_sb_norm sh_max_j_sbt core_product_sb ln_numf_HS6_UD_imp hhi_seas_sb_norm sh_max_seas_sbt seas_largest_sbt _cons ) order(    ) /// 
  stats(FE CTRL CTRL2 r2   N   , fmt( %~#s %~#s %~#s   2 %12.0fc  )  ///
  labels("FEs" "Controls" "Robustness"     `"\(R^{2}\)"'  "Obs."  ))   ///
  star(* 0.10 ** 0.05 *** 0.01) ///
  nonotes   

  
  
} 
